package cn.stylefeng.guns.modular.api.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.model.params.OuttaskApplyParam;
import cn.stylefeng.guns.modular.rms.model.params.OuttaskParam;
import cn.stylefeng.guns.modular.rms.model.result.OuttaskApplyResult;
import cn.stylefeng.guns.modular.rms.model.result.OuttaskResult;
import cn.stylefeng.guns.modular.rms.service.OuttaskApplyService;
import cn.stylefeng.guns.modular.rms.service.OuttaskService;
import cn.stylefeng.guns.modular.ums.entity.Member;
import cn.stylefeng.guns.modular.ums.service.MemberService;
import cn.stylefeng.guns.utils.MemberTokenUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 外包任务相关接口
 */
@Controller
@RequestMapping("/api/task")
@Api(tags = "外包任务相关接口")
public class ApiTaskController extends BaseController {

    @Autowired
    private OuttaskService taskService;
    @Autowired
    private OuttaskApplyService taskApplyService;
    @Autowired
    private MemberService memberService;

    /**
     * 查询外包任务列表
     */
    @ResponseBody
    @RequestMapping(value = "/getTaskList", method = RequestMethod.GET)
    @ApiOperation(value = "查询外包任务列表", notes = "查询外包任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getTaskList(OuttaskParam param) {
        LayuiPageInfo pageInfo = taskService.findPageBySpec(param);
        return ResponseData.success(pageInfo);
    }


    /**
     * 查询我的外包任务列表
     */
    @ResponseBody
    @RequestMapping(value = "/getMyTaskList", method = RequestMethod.GET)
    @ApiOperation(value = "查询我的外包任务列表", notes = "查询我的外包任务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getMyTaskList(OuttaskParam param) {
        long memberId = MemberTokenUtil.getMemberId();
        param.setMemberId(memberId);
        LayuiPageInfo pageInfo = taskService.findPageBySpec(param);
        return ResponseData.success(pageInfo);
    }

    /**
     * 查询外包任务详情
     */
    @ResponseBody
    @RequestMapping(value = "/getTaskDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查询外包任务详情", notes = "查询外包任务详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query")
    })
    public ResponseData getTaskDetail(Long id) {
        if (ObjectUtil.isEmpty(id)) {
            return ResponseData.error("id为空");
        }
        OuttaskParam param1 = new OuttaskParam();
        param1.setId(id);
        OuttaskResult result = taskService.findBySpec(param1);
        try {
            //查看是否已报名
            OuttaskApplyParam param = new OuttaskApplyParam();
            int memberId = MemberTokenUtil.getMemberId();
            param.setMemberId(memberId);
            param.setTaskId(id);
            OuttaskApplyResult ar = taskApplyService.findBySpec(param);
            if (ar != null) {
                result.setIfApply(true);
            } else {
                result.setIfApply(false);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return ResponseData.success(result);
    }

    /**
     * 添加/更新外包任务
     */
    @ResponseBody
    @RequestMapping(value = "/addOrUpdateTask", method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新外包任务", notes = "添加/更新外包任务")
    public ResponseData addOrUpdateTask(@RequestBody OuttaskParam param) {
        try {
            long memberId = MemberTokenUtil.getMemberId();
            Member member = memberService.getById(memberId);
            param.setMemberId(memberId);
            param.setCompanyId(member.getCompanyId());
            taskService.update(param);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.error(e.getMessage());
        }
    }

    /**
     * 删除外包任务
     */
    @ResponseBody
    @RequestMapping(value = "/deleteTask", method = RequestMethod.POST)
    @ApiOperation(value = "删除外包任务务", notes = "删除外包任务")
    public ResponseData deleteTask(@RequestBody OuttaskParam param) {
        try {
            taskService.removeById(param.getId());
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.error(e.getMessage());
        }
    }

    /**
     * 外包任务报名
     */
    @ResponseBody
    @RequestMapping(value = "/applyTask", method = RequestMethod.POST)
    @ApiOperation(value = "外包任务报名", notes = "外包任务报名")
    public ResponseData applyTask(@RequestBody OuttaskApplyParam param) {
        try {
            int memberId = MemberTokenUtil.getMemberId();
            param.setMemberId(memberId);
            taskApplyService.update(param);
            return ResponseData.success();
        } catch (Exception e) {
            return ResponseData.error(e.getMessage());
        }
    }

    /**
     * 查询报名信息
     */
    @ResponseBody
    @RequestMapping(value = "/getApplyTaskDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查询报名信息", notes = "查询报名信息，任务ID查询")
    public ResponseData getApplyTaskDetail(@RequestBody OuttaskApplyParam param) {
        try {
            int memberId = MemberTokenUtil.getMemberId();
            param.setMemberId(memberId);
            OuttaskApplyResult result = taskApplyService.findBySpec(param);
            return ResponseData.success(result);
        } catch (Exception e) {
            return ResponseData.error(e.getMessage());
        }
    }


    /**
     * 外包任务
     */
    @ResponseBody
    @RequestMapping(value = "/getApplyTaskList", method = RequestMethod.GET)
    @ApiOperation(value = "我的外包任务", notes = "我的外包任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getApplyTaskList(OuttaskApplyParam param) {
        if (param.getTaskId() == null) {
            int memberId = MemberTokenUtil.getMemberId();
            param.setMemberId(memberId);
        }
        LayuiPageInfo pageInfo = taskApplyService.findPageBySpec(param);
        return ResponseData.success(pageInfo);
    }

}



